/*     */ package com.zimbra.qa.unittest.prov.ldap;
/*     */ 
/*     */ import com.unboundid.ldap.sdk.Entry;
/*     */ import com.unboundid.ldap.sdk.LDAPConnection;
/*     */ import com.unboundid.ldap.sdk.LDAPConnectionOptions;
/*     */ import com.unboundid.ldap.sdk.LDAPResult;
/*     */ import com.unboundid.ldap.sdk.LDAPURL;
/*     */ import com.unboundid.ldap.sdk.SearchResultEntry;
/*     */ import java.io.PrintStream;
/*     */ import java.io.PrintWriter;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Date;
/*     */ import java.util.Hashtable;
/*     */ import javax.naming.CompositeName;
/*     */ import javax.naming.Context;
/*     */ import javax.naming.Name;
/*     */ import javax.naming.directory.Attributes;
/*     */ import javax.naming.directory.BasicAttribute;
/*     */ import javax.naming.directory.BasicAttributes;
/*     */ import javax.naming.directory.ModificationItem;
/*     */ import javax.naming.ldap.InitialLdapContext;
/*     */ import javax.naming.ldap.LdapContext;
/*     */ import org.apache.commons.cli.CommandLine;
/*     */ import org.apache.commons.cli.CommandLineParser;
/*     */ import org.apache.commons.cli.GnuParser;
/*     */ import org.apache.commons.cli.HelpFormatter;
/*     */ import org.apache.commons.cli.Options;
/*     */ import org.apache.commons.cli.ParseException;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestLdapReadTimeout
/*     */ {
/*     */   @Test
/*     */   public void noop()
/*     */     throws Exception
/*     */   {}
/*     */   
/*     */   private static abstract class LdapReadTimeoutTester
/*     */   {
/*     */     String uri;
/*     */     String bindDN;
/*     */     String password;
/*     */     
/*     */     protected LdapReadTimeoutTester(String uri, String bindDN, String password)
/*     */     {
/*  67 */       this.uri = uri;
/*  68 */       this.bindDN = bindDN;
/*  69 */       this.password = password;
/*     */     }
/*     */     
/*     */     abstract void doTest(String paramString, boolean paramBoolean) throws Exception;
/*     */   }
/*     */   
/*     */   private static class JNDITest extends TestLdapReadTimeout.LdapReadTimeoutTester
/*     */   {
/*     */     JNDITest(String uri, String bindDN, String password) {
/*  78 */       super(bindDN, password);
/*     */     }
/*     */     
/*     */     private LdapContext connect() throws Exception {
/*  82 */       Hashtable<String, String> env = new Hashtable();
/*  83 */       env.put("java.naming.factory.initial", "com.sun.jndi.ldap.LdapCtxFactory");
/*  84 */       env.put("java.naming.provider.url", this.uri);
/*  85 */       env.put("java.naming.referral", "follow");
/*  86 */       env.put("com.sun.jndi.ldap.connect.timeout", "30000");
/*  87 */       env.put("com.sun.jndi.ldap.read.timeout", "30000");
/*  88 */       env.put("com.sun.jndi.ldap.connect.pool", "false");
/*  89 */       env.put("java.naming.security.authentication", "simple");
/*  90 */       env.put("java.naming.security.principal", this.bindDN);
/*  91 */       env.put("java.naming.security.credentials", this.password);
/*     */       
/*  93 */       LdapContext dirCtxt = null;
/*  94 */       long startTime = 0L;
/*     */       try {
/*  96 */         startTime = System.currentTimeMillis();
/*  97 */         dirCtxt = new InitialLdapContext(env, null);
/*     */       } catch (Exception e) {
/*  99 */         SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss");
/* 100 */         long elapsed = System.currentTimeMillis() - startTime;
/* 101 */         System.out.println("Connection attempt started at: " + fmt.format(new Date(startTime)));
/* 102 */         System.out.println("\nActual elapsed time for making connection= " + elapsed + "ms");
/* 103 */         throw e;
/*     */       }
/*     */       
/* 106 */       return dirCtxt;
/*     */     }
/*     */     
/*     */     private void createEntry(LdapContext dirCtxt, String dn) throws Exception
/*     */     {
/* 111 */       Attributes attrs = new BasicAttributes(true);
/* 112 */       attrs.put("objectClass", "person");
/* 113 */       attrs.put("sn", "my sn");
/*     */       
/* 115 */       Context newCtxt = null;
/*     */       try {
/* 117 */         Name cpName = new CompositeName().add(dn);
/* 118 */         newCtxt = dirCtxt.createSubcontext(cpName, attrs);
/*     */       } finally {
/* 120 */         if (newCtxt != null)
/* 121 */           newCtxt.close();
/*     */       }
/*     */     }
/*     */     
/*     */     private void modifyEntry(LdapContext dirCtxt, String dn) throws Exception {
/* 126 */       Name cpName = new CompositeName().add(dn);
/*     */       
/* 128 */       BasicAttribute ba = new BasicAttribute("sn");
/* 129 */       ba.add("hello");
/*     */       
/* 131 */       ModificationItem[] mods = new ModificationItem[1];
/* 132 */       mods[0] = new ModificationItem(2, ba);
/* 133 */       dirCtxt.modifyAttributes(cpName, mods);
/*     */     }
/*     */     
/*     */     private void getEntry(LdapContext dirCtxt, String dn) throws Exception {
/* 137 */       Name cpName = new CompositeName().add(dn);
/* 138 */       dirCtxt.getAttributes(cpName);
/*     */     }
/*     */     
/*     */     public void doTest(String dn, boolean createEntry)
/*     */       throws Exception
/*     */     {
/* 144 */       LdapContext dirCtxt = null;
/*     */       
/*     */       try
/*     */       {
/* 148 */         dirCtxt = connect();
/*     */         
/*     */ 
/* 151 */         if (createEntry) {
/* 152 */           createEntry(dirCtxt, dn);
/* 153 */           System.out.println("Created entry: " + dn);
/*     */         }
/*     */         else {
/* 156 */           getEntry(dirCtxt, dn);
/*     */         }
/*     */       }
/*     */       finally
/*     */       {
/* 161 */         if (dirCtxt != null) {
/* 162 */           dirCtxt.close();
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static class UnboundIDTest extends TestLdapReadTimeout.LdapReadTimeoutTester {
/*     */     String host;
/*     */     int port;
/*     */     
/*     */     UnboundIDTest(String uri, String bindDN, String password) throws Exception {
/* 173 */       super(bindDN, password);
/*     */       
/* 175 */       LDAPURL ldapUrl = new LDAPURL(uri);
/* 176 */       this.host = ldapUrl.getHost();
/* 177 */       this.port = ldapUrl.getPort();
/*     */     }
/*     */     
/*     */     LDAPConnection connect() throws Exception {
/* 181 */       LDAPConnectionOptions connOpts = new LDAPConnectionOptions();
/* 182 */       connOpts.setUseSynchronousMode(true);
/* 183 */       connOpts.setConnectTimeoutMillis(30000);
/* 184 */       connOpts.setResponseTimeoutMillis(30000L);
/*     */       
/* 186 */       LDAPConnection conn = null;
/* 187 */       long startTime = 0L;
/*     */       try {
/* 189 */         startTime = System.currentTimeMillis();
/* 190 */         conn = new LDAPConnection(connOpts, this.host, 389, this.bindDN, this.password);
/*     */       }
/*     */       catch (Exception e)
/*     */       {
/* 194 */         SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss");
/*     */         
/* 196 */         long elapsed = System.currentTimeMillis() - startTime;
/*     */         
/* 198 */         System.out.println("Connection attempt started at: " + fmt.format(new Date(startTime)));
/* 199 */         System.out.println("\nActual elapsed time for making connection = " + elapsed + "ms");
/* 200 */         throw e;
/*     */       }
/*     */       
/* 203 */       return conn;
/*     */     }
/*     */     
/*     */     private void createEntry(LDAPConnection conn, String dn) throws Exception {
/* 207 */       Entry entry = new Entry(dn);
/* 208 */       entry.addAttribute("objectClass", "person");
/* 209 */       entry.addAttribute("sn", "my sn");
/*     */       
/* 211 */       LDAPResult result = conn.add(entry);
/*     */     }
/*     */     
/*     */     private void getEntry(LDAPConnection conn, String dn) throws Exception
/*     */     {
/* 216 */       SearchResultEntry entry = conn.getEntry(dn);
/*     */     }
/*     */     
/*     */     public void doTest(String dn, boolean createEntry)
/*     */       throws Exception
/*     */     {
/* 222 */       LDAPConnection conn = null;
/*     */       
/*     */       try
/*     */       {
/* 226 */         conn = connect();
/*     */         
/*     */ 
/* 229 */         if (createEntry) {
/* 230 */           createEntry(conn, dn);
/* 231 */           System.out.println("Created entry: " + dn);
/*     */         }
/*     */         else {
/* 234 */           getEntry(conn, dn);
/*     */         }
/*     */       } finally {
/* 237 */         if (conn != null) {
/* 238 */           conn.close();
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static void printSysInfo() throws Exception {
/* 245 */     System.out.println("System property java.home=" + System.getProperty("java.home"));
/* 246 */     System.out.println("System property java.runtime.version=" + System.getProperty("java.runtime.version"));
/* 247 */     System.out.println("System property java.version=" + System.getProperty("java.version"));
/* 248 */     System.out.println("System property java.vm.info=" + System.getProperty("java.vm.info"));
/* 249 */     System.out.println("System property java.vm.name=" + System.getProperty("java.vm.name"));
/* 250 */     System.out.println("System property java.vm.version=" + System.getProperty("java.vm.version"));
/* 251 */     System.out.println("System property os.arch=" + System.getProperty("os.arch"));
/* 252 */     System.out.println("System property os.name=" + System.getProperty("os.name"));
/* 253 */     System.out.println("System property os.version=" + System.getProperty("os.version"));
/* 254 */     System.out.println("System property sun.arch.data.model=" + System.getProperty("sun.arch.data.model"));
/* 255 */     System.out.println("System property sun.cpu.endian=" + System.getProperty("sun.cpu.endian"));
/*     */     
/*     */ 
/* 258 */     System.out.println();
/*     */   }
/*     */   
/*     */   private static void test(LdapReadTimeoutTester tester) {
/* 262 */     Date date = new Date();
/* 263 */     SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd-HHmmss");
/* 264 */     String testId = fmt.format(date);
/* 265 */     String dn = "cn=" + testId;
/*     */     
/* 267 */     int i = 1;
/*     */     try {
/* 269 */       printSysInfo();
/*     */       
/*     */ 
/* 272 */       tester.doTest(dn, true);
/*     */       
/*     */       for (;;)
/*     */       {
/* 276 */         tester.doTest(dn, false);
/*     */         
/*     */ 
/* 279 */         if (i % 100 == 0)
/* 280 */           System.out.println("Tested " + i + " iterations");
/* 281 */         i++;
/*     */       }
/*     */     } catch (Exception e) {
/* 284 */       System.out.println("Failed at iteration: " + i);
/* 285 */       System.out.println();
/* 286 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */   
/* 290 */   private static String O_HELP = "h";
/* 291 */   private static String O_SLEEP = "s";
/* 292 */   private static String O_URI = "H";
/* 293 */   private static String O_BINDDN = "D";
/* 294 */   private static String O_PASSWORD = "w";
/*     */   
/*     */   private static void usage(Options options) {
/* 297 */     System.out.println("\n");
/* 298 */     PrintWriter pw = new PrintWriter(System.out, true);
/* 299 */     HelpFormatter formatter = new HelpFormatter();
/* 300 */     formatter.printHelp(pw, formatter.getWidth(), "zmjava " + TestLdapReadTimeout.class.getCanonicalName() + " [options]", null, options, formatter.getLeftPadding(), formatter.getDescPadding(), null);
/*     */     
/*     */ 
/* 303 */     System.out.println("\n");
/* 304 */     pw.flush();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void main(String[] args)
/*     */     throws Exception
/*     */   {
/* 316 */     Options options = new Options();
/* 317 */     options.addOption(O_HELP, false, "print usage");
/* 318 */     options.addOption(O_SLEEP, true, "upon hitting an Exception, minutes to wait until exiting the program.  If not specified, will exit immediately.");
/*     */     
/*     */ 
/* 321 */     options.addOption(O_URI, true, "URI. e.g. ldap://localhost:389");
/* 322 */     options.addOption(O_BINDDN, true, "bind DN");
/* 323 */     options.addOption(O_PASSWORD, true, "password");
/*     */     
/* 325 */     CommandLine cl = null;
/*     */     try {
/* 327 */       CommandLineParser parser = new GnuParser();
/* 328 */       cl = parser.parse(options, args);
/* 329 */       if (cl == null) {
/* 330 */         throw new ParseException("");
/*     */       }
/*     */     } catch (ParseException e) {
/* 333 */       usage(options);
/* 334 */       e.printStackTrace();
/* 335 */       System.exit(1);
/*     */     }
/*     */     
/* 338 */     if (cl.hasOption(O_HELP)) {
/* 339 */       usage(options);
/* 340 */       System.exit(0);
/*     */     }
/*     */     
/* 343 */     String uri = null;
/* 344 */     String bindDN = null;
/* 345 */     String password = null;
/* 346 */     Integer minutesToWait = null;
/*     */     
/* 348 */     if (cl.hasOption(O_URI)) {
/* 349 */       uri = cl.getOptionValue(O_URI);
/*     */     } else {
/* 351 */       uri = "ldap://localhost:389";
/*     */     }
/*     */     
/* 354 */     if (cl.hasOption(O_BINDDN)) {
/* 355 */       bindDN = cl.getOptionValue(O_BINDDN);
/*     */     } else {
/* 357 */       bindDN = "uid=zimbra,cn=admins,cn=zimbra";
/*     */     }
/*     */     
/* 360 */     if (cl.hasOption(O_PASSWORD)) {
/* 361 */       password = cl.getOptionValue(O_PASSWORD);
/*     */     } else {
/* 363 */       password = "zimbra";
/*     */     }
/*     */     
/* 366 */     if (cl.hasOption(O_SLEEP)) {
/* 367 */       String wait = cl.getOptionValue(O_SLEEP);
/*     */       try
/*     */       {
/* 370 */         minutesToWait = Integer.valueOf(wait);
/*     */       } catch (NumberFormatException e) {
/* 372 */         usage(options);
/* 373 */         e.printStackTrace();
/* 374 */         System.exit(1);
/*     */       }
/*     */     }
/*     */     
/* 378 */     LdapReadTimeoutTester tester = null;
/*     */     
/*     */     try
/*     */     {
/* 382 */       tester = new UnboundIDTest(uri, bindDN, password);
/*     */       
/* 384 */       System.out.println("=============");
/* 385 */       System.out.println(tester.getClass().getCanonicalName());
/* 386 */       System.out.println("LDAP server URI: " + uri);
/* 387 */       System.out.println("bind DN: " + bindDN);
/* 388 */       System.out.println("=============");
/* 389 */       System.out.println();
/*     */     }
/*     */     catch (Exception e) {
/* 392 */       e.printStackTrace();
/* 393 */       System.exit(1);
/*     */     }
/*     */     
/* 396 */     long startTime = System.currentTimeMillis();
/* 397 */     test(tester);
/* 398 */     long endTime = System.currentTimeMillis();
/* 399 */     long elapsed = endTime - startTime;
/*     */     
/* 401 */     SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/*     */     
/* 403 */     System.out.println();
/* 404 */     System.out.println(tester.getClass().getCanonicalName());
/* 405 */     System.out.println("Started at: " + fmt.format(new Date(startTime)));
/* 406 */     System.out.println("Ended at: " + fmt.format(new Date(endTime)));
/* 407 */     System.out.println("Elapsed = " + elapsed / 1000L + " seconds");
/* 408 */     System.out.println();
/*     */     
/* 410 */     if (minutesToWait != null) {
/* 411 */       System.out.println("Sleeping for " + minutesToWait + " minutes before exiting...");
/* 412 */       Thread.sleep(minutesToWait.intValue() * 60 * 1000);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/TestLdapReadTimeout.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */